import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Iterator;

import org.jhotdraw.figures.ArrowTip;
import org.jhotdraw.figures.ConnectedTextTool;
import org.jhotdraw.figures.LineConnection;
import org.jhotdraw.figures.TextFigure;
import org.jhotdraw.figures.TextTool;
import org.jhotdraw.framework.Connector;
import org.jhotdraw.framework.DrawingEditor;
import org.jhotdraw.framework.DrawingView;
import org.jhotdraw.framework.Figure;
import org.jhotdraw.standard.TextHolder;

public class Transitions extends LineConnection {

	/*This constructor allows the Transition to be drawn with one arrow by
	overriding the constructor of LineConnection which draws a line with two
	arrow heads.*/
	
	private TextFigure _tf = new TextFigure();
	
	public Transitions() {
		setStartDecoration(null);
		setEndDecoration(new ArrowTip());
		this._tf.setText("");
	}
	
	@Override
	public void draw(Graphics g)
	{
		super.draw(g);
		if (!this._tf.getText().equals(""))
		{
			StateFigure startFigure = (StateFigure)startFigure();
			StateFigure endFigure = (StateFigure)endFigure();
			Rectangle rect = startFigure.displayBox();
			Rectangle rect2 = endFigure.displayBox();
			this.connectStart(startFigure.connectorAt(rect2.x + rect2.width/2, rect2.y + rect2.height/2));
			this.connectEnd(endFigure.connectorAt(rect.x + rect.width/2, rect.y + rect.height/2));
			this._tf.moveBy(-this._tf.displayBox().x, -this._tf.displayBox().y);
			this._tf.moveBy((rect.x + rect.width/2 + rect2.x + rect2.width/2)/2, (rect.y + rect.height/2 + rect2.y + rect2.height/2)/2);
			if (this.startPoint().equals(this.endPoint()))
			{
				g.drawArc(this.startPoint().x - 20, this.startPoint().y - 40, 40, 40, -90, 340);
				this._tf.moveBy(-this._tf.displayBox().x, -this._tf.displayBox().y);
				this._tf.moveBy(this.startPoint().x, this.startPoint().y - 55);
			}
			this.updateConnection();
		}
	}
	
	public Transitions(StateFigure startState, StateFigure endState, String letter, DrawingView window)
	{
		Rectangle rect = startState.displayBox();
		Rectangle rect2 = endState.displayBox();
		this._tf = new TextFigure();
		window.add(this._tf);
		this._tf.setText(letter);
		if (letter.equals("")) this._tf.setText("\u03B5");
		this._tf.moveBy((rect.x + rect.width/2 + rect2.x + rect2.width/2)/2, (rect.y + rect.height/2 + rect2.y + rect2.height/2)/2);
		setStartDecoration(null);
		setEndDecoration(new ArrowTip());
		this.connectStart(startState.connectorAt(rect2.x + rect2.width/2, rect2.y  + rect2.height/2));
		this.connectEnd(endState.connectorAt(rect.x + rect.width/2, rect.y + rect.height/2));
		if (startState.equals(endState))
		{
			this._tf.moveBy(-this._tf.displayBox().x, -this._tf.displayBox().y);
			this._tf.moveBy(startState.displayBox().x + startState.displayBox().width/2, startState.displayBox().y - 55);
		}
		this.updateConnection();
	}
}